package org.jeecg.modules.mobile.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.jeecg.modules.mobile.entity.MobileConfiguration;
import org.jeecg.modules.mobile.mapper.MobileConfigurationMapper;
import org.jeecg.modules.mobile.service.IMobileConfigurationService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description: 移动端服务器配置
 * @Author: jeecg-boot
 * @Date:   2023-08-22
 * @Version: V1.0
 */
@Service
public class MobileConfigurationServiceImpl extends ServiceImpl<MobileConfigurationMapper, MobileConfiguration> implements IMobileConfigurationService {

    /**
     * 保存配置
     * @param mobileConfiguration
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveConfiguration(MobileConfiguration mobileConfiguration) {
        List<MobileConfiguration> list = this.list(new LambdaQueryWrapper<MobileConfiguration>()
                .eq(MobileConfiguration::getUseFlag, "1"));
        for (MobileConfiguration configuration : list) {
            configuration.setUseFlag("0");
        }
        this.updateBatchById(list);

        MobileConfiguration one = this.getOne(new LambdaQueryWrapper<MobileConfiguration>()
                .eq(MobileConfiguration::getAddress, mobileConfiguration.getAddress())
                .last("limit 1"));
        if (one != null) {
            one.setUseFlag("1");
            this.updateById(one);
        } else {
            mobileConfiguration.setId(null);
            mobileConfiguration.setUseFlag("1");
            this.save(mobileConfiguration);
        }
    }
}
